package plugin.voldemort.lookup;

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.FileDialog;
import org.pentaho.di.core.Const;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.ui.core.widget.TextVar;
import org.pentaho.di.ui.trans.step.BaseStepDialog;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDialogInterface;
import org.pentaho.ui.database.Messages;

public class LookupStepDialog extends BaseStepDialog implements StepDialogInterface {

	private static Class<?> PKG = LookupStepMeta.class; // for i18n purposes
	
	private LookupStepMeta input;

	// connection settings widgets
	private Label wlVoldemortHost;	//Path File
	private TextVar wVoldemortHost;	
	private Label wlVoldemortPort;	//Consulta
	private TextVar wVoldemortPort;
	
	private Button wbbFilename;
	
	private Button wInclURL;
	private FormData fdInclURLField;
	//private TextFileInput aa;
	
	// constructor
	public LookupStepDialog(Shell parent, Object in, TransMeta transMeta, String sname) {
		super(parent, (BaseStepMeta) in, transMeta, sname);
		input = (LookupStepMeta) in;
	}

	// builds and shows the dialog 
	public String open() {
		Shell parent = getParent();
		Display display = parent.getDisplay();

		shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MIN | SWT.MAX);
		props.setLook(shell);
		setShellImage(shell, input);

		ModifyListener lsMod = new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				input.setChanged();
			}
		};
		backupChanged = input.hasChanged();

		FormLayout formLayout = new FormLayout();
		formLayout.marginWidth = Const.FORM_MARGIN;
		formLayout.marginHeight = Const.FORM_MARGIN;

		shell.setLayout(formLayout);
		shell.setText(BaseMessages.getString(PKG, "VoldemortDialog.Shell.Title")); 

		int middle = props.getMiddlePct();
		int margin = Const.MARGIN;

	    /*************************************************
        // STEP NAME ENTRY
		*************************************************/
		
		// Stepname line
		wlStepname = new Label(shell, SWT.RIGHT);
		wlStepname.setText(BaseMessages.getString(PKG, "System.Label.StepName")); 
		props.setLook(wlStepname);
		fdlStepname = new FormData();
		fdlStepname.left = new FormAttachment(0, 0);
		fdlStepname.right = new FormAttachment(middle, -margin);
		fdlStepname.top = new FormAttachment(0, margin);
		wlStepname.setLayoutData(fdlStepname);
		
		wStepname = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
		wStepname.setText(stepname);
		props.setLook(wStepname);
		wStepname.addModifyListener(lsMod);
		fdStepname = new FormData();
		fdStepname.left = new FormAttachment(middle, 0);
		fdStepname.top = new FormAttachment(0, margin);
		fdStepname.right = new FormAttachment(100, 0);
		wStepname.setLayoutData(fdStepname);
		
	    /*************************************************
        // VOLDEMORT CONNECTION GROUP
		*************************************************/
		/*
        Group gConnect = new Group(shell, SWT.);
        gConnect.setText(BaseMessages.getString(PKG, "VoldemortDialog.ConnectGroup.Label")); 
        FormLayout gConnectLayout = new FormLayout();
        
        //FormData fdlVoldemortHost2 = new FormData();
        //fdlVoldemortHost2.top = new FormAttachment(50, 0);
        //fdlVoldemortHost2.left = new FormAttachment(0, 0);
        //fdlVoldemortHost2.right = new FormAttachment(middle, -20);
        gConnectLayout.marginWidth = 2;
        gConnectLayout.marginTop=100;
        gConnectLayout.marginHeight = 100;
        
        //gConnect.setLayoutData(fdlVoldemortHost2);
        gConnect.setLayoutData(gConnectLayout);
        props.setLook(gConnect);*/

        
        
        
        /*wInclURL.setLayoutData(wInclURL);
        wInclURL = new Button(gConnect, SWT.CHECK);
		props.setLook(wInclURL);
		wInclURL.setToolTipText(BaseMessages.getString("SalesforceInputDialog.InclURL.Tooltip"));
		fdInclURLField = new FormData();
		fdInclURLField.left = new FormAttachment(middle, 0);
		fdInclURLField.top = new FormAttachment(0, 3*margin);
		wInclURL.setLayoutData(fdInclURLField);
		wInclURL.addSelectionListener(new SelectionAdapter() 
		{
			public void widgetSelected(SelectionEvent e) 
			{
				FileDialog fd = new FileDialog(shell, SWT.OPEN);
		        fd.setText("Open");
		        fd.setFilterPath("C:/");
		        String[] filterExt = { "*.txt", "*.doc", ".rtf", "*.*" };
		        fd.setFilterExtensions(filterExt);
		        String selected = fd.open();
		        System.out.println(selected);
			}
		}
	);*/

        /*
        wOK = new Button(shell, SWT.PUSH);
        wOK.setText(BaseMessages.getString(PKG, "System.Button.OK")); 
		
        Listener lsOpen = new Listener() {
			public void handleEvent(Event e) {
				openB();
			}
		};

		wOK.addListener(SWT.Selection, lsOpen);*/
        /*
        FileDialog fd = new FileDialog(shell, SWT.OPEN);
        fd.setText("Open");
        fd.setFilterPath("C:/");
        String[] filterExt = { "*.txt", "*.doc", ".rtf", "*.*" };
        fd.setFilterExtensions(filterExt);
        String selected = fd.open();
        System.out.println(selected);*/
        
		// Path File
        wlVoldemortHost = new Label(shell, SWT.RIGHT);
        wlVoldemortHost.setText(BaseMessages.getString(PKG, "VoldemortDialog.Host.Label")); 
        props.setLook(wlVoldemortHost);
        FormData fdlVoldemortHost = new FormData();
        fdlVoldemortHost.top = new FormAttachment(21, 0);
        fdlVoldemortHost.left = new FormAttachment(0, 0);
        fdlVoldemortHost.right = new FormAttachment(middle, -margin);
        wlVoldemortHost.setLayoutData(fdlVoldemortHost);
        
        wVoldemortHost = new TextVar(transMeta, shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
        wVoldemortHost.addModifyListener(lsMod);
        wVoldemortHost.setToolTipText(BaseMessages.getString(PKG, "VoldemortDialog.Host.Tooltip")); 
        props.setLook(wVoldemortHost);
        FormData fdVoldemortHost = new FormData();
        fdVoldemortHost.top = new FormAttachment(20, margin);
        fdVoldemortHost.left = new FormAttachment(middle, 0);
        fdVoldemortHost.right = new FormAttachment(75, 0);
        wVoldemortHost.setLayoutData(fdVoldemortHost);
        
 		// The filename browse button
         wbbFilename=new Button(shell, SWT.CENTER);
         props.setLook(wbbFilename);
         wbbFilename.setText(BaseMessages.getString(PKG, "System.Button.Browse"));
         //wbbFilename.setToolTipText(Messages.getString("System.Tooltip.BrowseForFileOrDirAndAdd"));
         FormData fdbFilename = new FormData();
         fdbFilename.top  = new FormAttachment(20, margin);
         fdbFilename.right= new FormAttachment(75, 78);
         wbbFilename.setLayoutData(fdbFilename);

        // Consulta
        wlVoldemortPort = new Label(shell, SWT.RIGHT);
        wlVoldemortPort.setText(BaseMessages.getString(PKG, "VoldemortDialog.Port.Label")); 
        props.setLook(wlVoldemortPort);
        FormData fdlVoldemortPassword = new FormData();
        fdlVoldemortPassword.top = new FormAttachment(wVoldemortHost, margin);
        fdlVoldemortPassword.left = new FormAttachment(0, 0);
        fdlVoldemortPassword.right = new FormAttachment(middle, -margin);
        wlVoldemortPort.setLayoutData(fdlVoldemortPassword);
        
        wVoldemortPort = new TextVar(transMeta, shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
        wVoldemortPort.addModifyListener(lsMod);
        wVoldemortPort.setToolTipText(BaseMessages.getString(PKG, "VoldemortDialog.Port.Tooltip")); 
        props.setLook(wVoldemortPort);
        FormData fdVoldemortPassword = new FormData();
        fdVoldemortPassword.top = new FormAttachment(wVoldemortHost, margin);
        fdVoldemortPassword.left = new FormAttachment(middle, 0);
        fdVoldemortPassword.right = new FormAttachment(75, 0);
        wVoldemortPort.setLayoutData(fdVoldemortPassword);
        
	    /*************************************************
        // OK AND CANCEL BUTTONS 
		*************************************************/

		wOK = new Button(shell, SWT.CENTER);
		props.setLook(wOK);
		wOK.setText(BaseMessages.getString(PKG, "System.Button.OK"));
		FormData fdbOK = new FormData();
		fdbOK.top  = new FormAttachment(40, margin);
		fdbOK.right= new FormAttachment(middle+15, -margin);
		wOK.setLayoutData(fdbOK);
        
		wCancel = new Button(shell, SWT.CENTER);
		props.setLook(wCancel);
		wCancel.setText(BaseMessages.getString(PKG, "System.Button.Cancel")); 
		FormData fdbCancel = new FormData();
		fdbCancel.top  = new FormAttachment(40, margin);
		fdbCancel.right= new FormAttachment(middle+30, -margin);
		wCancel.setLayoutData(fdbCancel);
		//wOpen2 = new Button(shell, SWT.PUSH);

		//BaseStepDialog.positionBottomButtons(shell, new Button[] { wOK, wCancel }, margin, wKeys);

		
		// Add listeners
		lsCancel = new Listener() {
			public void handleEvent(Event e) {
				cancel();
			}
		};
		lsOK = new Listener() {
			public void handleEvent(Event e) {
				ok();
			}
		};

		wCancel.addListener(SWT.Selection, lsCancel);
		wOK.addListener(SWT.Selection, lsOK);

	    /*************************************************
        // DEFAULT ACTION LISTENERS 
		*************************************************/
		
		lsDef = new SelectionAdapter() {
			public void widgetDefaultSelected(SelectionEvent e) {
				ok();
			}
		};

		wStepname.addSelectionListener(lsDef);
		wVoldemortHost.addSelectionListener(lsDef);
		wVoldemortPort.addSelectionListener(lsDef);
		//wVoldemortStore.addSelectionListener(lsDef);

		// Detect X or ALT-F4 or something that kills this window...
		shell.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent e) {
				cancel();
			}
		});

		
		// Set the shell size, based upon previous time...
		setSize();

		//if (wbbFilename!=null) {
		// Listen to the browse button next to the file name
		wbbFilename.addSelectionListener(
				new SelectionAdapter()
				{
					public void widgetSelected(SelectionEvent e) 
					{
						FileDialog dialog = new FileDialog(shell, SWT.OPEN);
						dialog.setFilterExtensions(new String[] {"*.txt;*.csv", "*.csv", "*.txt", "*"});
						if (wVoldemortHost.getText()!=null)
						{
							String fname = transMeta.environmentSubstitute(wVoldemortHost.getText());
							dialog.setFileName( fname );
						}
						
						dialog.setFilterNames(new String[] {Messages.getString("System.FileType.CSVFiles")+", "+
								                            Messages.getString("System.FileType.TextFiles"), 
								                            Messages.getString("System.FileType.CSVFiles"), 
								                            Messages.getString("System.FileType.TextFiles"), 
								                            Messages.getString("System.FileType.AllFiles")});
						
						if (dialog.open()!=null)
						{
							String str = dialog.getFilterPath()+System.getProperty("file.separator")+dialog.getFileName();
							wVoldemortHost.setText(str);
						}
					}
				}
			);
	//}
	    /*************************************************
        // POPULATE AND OPEN DIALOG
		*************************************************/
		
		getData();
		//setComboValues();
		
		input.setChanged(backupChanged);

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		
		
		////////////////
		//////////////////
		///////////////////
		///////////////////
		
		return stepname;
	}
	
	// Collect data from the meta and place it in the dialog
	public void getData() {
		
		wStepname.selectAll();
		
		if (input.getVoldemortHost() != null){
			wVoldemortHost.setText(input.getVoldemortHost());	
		}
		
		if (input.getVoldemortPort() != null){
			wVoldemortPort.setText(input.getVoldemortPort());	
		}
	}
	
	private void cancel() {
		stepname = null;
		input.setChanged(backupChanged);
		dispose();
	}
	/*
	private void openB() {
		FileDialog fd = new FileDialog(shell, SWT.OPEN);
        fd.setText("Open");
        fd.setFilterPath("C:/");
        String[] filterExt = { "*.txt", "*.doc", ".rtf", "*.*" };
        fd.setFilterExtensions(filterExt);
        String selected = fd.open();
        System.out.println(selected);
	}
	*/
	// let the meta know about the entered data
	private void ok() {
		stepname = wStepname.getText(); // return value
		
		input.setVoldemortHost(wVoldemortHost.getText());
		input.setVoldemortPort(wVoldemortPort.getText());
		dispose();
	}
}
